package week6.day37;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author fengzihao
 * @Date 2024-05-21 19:52
 */
public class FunctionRealization {
    public static void main(String[] args) {
        // 获取交易记录信息
        List<Transaction> transactions = TransactionRecord.transactions;
        // 找出2011年发生的交易记录, 按照从低到高交易额排序
        List<Transaction> collect1 = transactions.stream().filter(transaction -> 2011 == transaction.getYear()).sorted(Comparator.comparingInt(Transaction::getValue)).collect(Collectors.toList());
        System.out.println("collect1 = " + collect1);
        // 找出交易员都在哪些不同的城市工作过的城市
        List<String> collect2 = transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct().collect(Collectors.toList());
        System.out.println("collect2 = " + collect2);
        // 查找来自北京的交易, 按交易员姓名排序
        List<Transaction> collect3 = transactions.stream().filter(transaction -> "beijing".equals(transaction.getTrader().getCity())).sorted(Comparator.comparing(transaction -> transaction.getTrader().getName())).collect(Collectors.toList());
        System.out.println("collect3 = " + collect3);
        // 返回一个字符串包含所有交易员, 字符串的顺序按照姓名排序
        Optional<String> reduce = transactions.stream().sorted(Comparator.comparing(transaction -> transaction.getTrader().getName())).map(transaction -> transaction.getTrader().getName()).distinct().reduce((name1, name2) -> name1 + " " + name2);
        reduce.ifPresent(string -> System.out.println("name = " + string));
        // 判断有没有交易员在北京工作
        boolean b = transactions.stream().anyMatch(transaction -> "beijing".equals(transaction.getTrader().getCity()));
        System.out.println("是否有人在北京工作 = " + b);
        // 打印所有发生在北京的交易记录金额
        List<Integer> collect = transactions.stream().filter(transaction -> "beijing".equals(transaction.getTrader().getCity())).map(transaction -> transaction.getValue()).collect(Collectors.toList());
        System.out.println("所有发生在北京的交易记录金额 = " + collect);
        // 所有交易中最高的交易是多少
        Optional<Transaction> first1 = transactions.stream().sorted((transaction1, transaction2) -> transaction2.getValue() - transaction1.getValue()).findFirst();
        first1.ifPresent(transaction -> System.out.println("所有交易中最高的交易 = " + transaction.getValue()));
        // 所有交易中, 交易最低的交易信息
        Optional<Transaction> first2 = transactions.stream().sorted((transaction1, transaction2) -> transaction1.getValue() - transaction2.getValue()).findFirst();
        first2.ifPresent(transaction -> System.out.println("所有交易中最低的交易 = " + transaction.getValue()));
    }
}
